package com.libweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libweb.model.dao.BookDao;
import com.libweb.model.dao.MemberDao;
import com.libweb.model.dto.Book;
import com.libweb.model.dto.Member;

@WebServlet("/book/update.action")
public class BookUpdateServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		//1. Board 객체를 만들고 사용자가 입력한 데이터를 저장
		Book book = new Book();
		book.setBookNo(Integer.parseInt(req.getParameter("bookno")));
		book.setTitle(req.getParameter("title"));
		book.setPublisher(req.getParameter("publisher"));
		book.setAuthor(req.getParameter("author"));
		
		String pubDateString = req.getParameter("pubdate");
		int year = Integer.parseInt(pubDateString.substring(0, 4));
		int month = Integer.parseInt(pubDateString.substring(5, 7));
		int day = Integer.parseInt(pubDateString.substring(8));
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, day);
		
		Date pubDate = new Date(c.getTimeInMillis());
		book.setPubDate(pubDate);

		//2. 데이터베이스에 변경된 내용 적용
		BookDao dao = new BookDao();
		dao.updateBook(book);
		
		//3. 홈 페이지로 이동
		resp.sendRedirect("/libweb/book/list.action");	
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		doGet(req, resp);
	}
	
}






